import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.PreparedStatement;
import java.sql.SQLException;


public class AddCity {
    public static void main(String[] args) throws SQLException {
        //1.获得数据源 DataSource(设置 MySQL 的服务器地址)
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?characterEncoding=utf8&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("12345678");
        //2.
        Connection connection = (Connection) dataSource.getConnection();
        //3.
        String insertSQL = "insert into city(id,name) values(?,?)";
        PreparedStatement statement = connection.prepareStatement(insertSQL);
        statement.setInt(1,4);
        statement.setString(2,"广东");
        //4.
        int result = statement.executeUpdate();
        System.out.println("受影响的行数: " + result);
        //5.
        statement.close();//关闭执行器
        connection.close();//关闭连接
    }
}